<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 86131
  Date: 2022/4/4
  Time: 16:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>

<html>
<head>
    <title>学生列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.5/css/layui.css">
    <script src="https://www.layuicdn.com/layui-v2.5.5/layui.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
        input{
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<%--弹出层--%>
<div id="edit_stu_box" hidden="hidden" style="background-color: #f3f3f0;width: 450px;height: 400px;
                                             position: absolute; margin-left: 34%;
                                             margin-top: 30px;z-index: 999;border-radius: 15px" >
    <button id="close_edit_stu_box" onclick="closeBox()" style="font-size: 20px">×</button>
    <div style="position: relative;margin-left: 125px;margin-top: 23px">
        <h2 style="margin-left: 70px;margin-bottom: 8px">修改</h2>
        <form id="edit_box">
            学号:<input type="text" name="stu_id" placeholder="学号" id="stu_id" readonly="readonly"><br>
            姓名:<input type="text" name="stu_name" placeholder="姓名" id="stu_name" readonly="readonly"><br>
            性别:<input type="radio" name="stu_sex" value="男" title="男" checked="">男
            <input type="radio" name="stu_sex" value="女" title="女"> 女<br>
            语文:<input type="text" name="chinese" placeholder="语文" id="chinese" ><br>
            数学:<input type="text" name="math" placeholder="数学" id="math" ><br>
            英语:<input type="text" name="english" placeholder="英语" id="english" ><br>
            理综:<input type="text" name="sci_total" placeholder="理综" id="sci_total" ><br>
            总分:<input type="text" name="total" placeholder="总分" id="total" readonly="readonly"><br>
            <button type="button" class="btn btn-default" title="修改" data-toggle="modal"
                    data-target="#addOrEditModal" onclick="editStudent()" style="margin-left: 30px"> 修改信息
            </button>
        </form>


        <button class="" onclick="total()"  style="margin-left: 120px;margin-top: -22px;background-color: darkcyan;color: white">计算总分</button>
    </div>
</div>
<%--学生列表--%>
<div id="table_box" style="margin-left: 27%">
    <table id="stu" style="border: #13E8E9" border="2" width="60%">
       <thead>
       <tr align="center">
           <td>学号</td>
           <td>姓名</td>
           <td>性别</td>
           <td>语文</td>
           <td>数学</td>
           <td>英语</td>
           <td>理综</td>
           <td>总分</td>
           <td>操作</td>
       </tr>
       </thead>
       <tbody>
       <c:forEach items="${stus.rows}" var="stu">
          <tr>
              <td>${stu.stu_id}</td>
              <td width="150px">${stu.stu_name}</td>
              <td>${stu.stu_sex}</td>
              <td>${stu.chinese}</td>
              <td>${stu.math}</td>
              <td>${stu.english}</td>
              <td>${stu.sci_total}</td>
              <td>${stu.total}</td>
              <td>
                  <button name="delet" onclick="del('${stu.stu_id}')">删除</button>
                  <button name="update" onclick="update('${stu.stu_id}','${stu.stu_name}',
                                                        '${stu.chinese}','${stu.math}',
                                                        '${stu.english}','${stu.sci_total}')">编辑
                  </button>
              </td>
          </tr>
       </c:forEach>
       </tbody>
    </table>

</div>
<script>
    //获取当前项目的名称
    function getProjectPath() {

        var pathName = window.document.location.pathname;

        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        return  projectName;
    }

    function del(id) {
        var r=confirm("确定删除"+id+"的所有成绩?");
        if (r==true)
        {
            var url = "${pageContext.request.contextPath}/delStudent?stu_id="+id;
            alert(url);
            $.ajax({
                url:url,
                type:"post",
                success:function () {
                    alert("删除成功!");
                }
            })
        }
        else
        {
            alert("删除取消!");
        }
    }

    function update(stu_id,stu_name,chinese,math,english,sci_total) {
        $("#edit_stu_box").show();
        $("#stu_id").val(stu_id);
        $("#stu_name").val(stu_name);
        $("#chinese").val(chinese);
        $("#math").val(math);
        $("#english").val(english);
        $("#sci_total").val(sci_total);
    }
    function closeBox() {
        $("#edit_stu_box").hide();
    }
    function total() {
        var chinese=parseInt($("#chinese").val());
        var math=parseInt($("#math").val());
        var english=parseInt($("#english").val());
        var sci_tatal=parseInt($("#sci_total").val());
        var sum=chinese+math+english+sci_tatal;
        var total=$("#total").val(sum);
    }
    function editStudent() {
        $("#edit_stu_box").hide();
        var url = "${pageContext.request.contextPath}/editStudent";
        $.post(url, $("#edit_box").serialize(), function () {
            alert("修改成功!");
        })
    }
</script>

</body>
</html>
